package com.situ.hospital.room.servlet;

import java.io.IOException;
import java.sql.SQLException;
import java.util.HashMap;
import java.util.Map;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.json.JSONObject;

import com.situ.hospital.room.model.RoomModel;
import com.situ.hospital.room.service.RoomService;

/**
 * Servlet implementation class RoomServlet
 */
@WebServlet("/RoomServlet")
public class RoomServlet extends HttpServlet {
	private static final long serialVersionUID = 1L;
    private RoomService service=new RoomService();
   
	/**
	 * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		String action = request.getParameter("action");

		switch (action) {
		case "add":
			add(request, response);
			break;
		case "del":
			del(request, response);
			break;
		case "selectmodel":
			selectmodel(request, response);
			break;
		case "upd":
			upd(request,response);
			break;
		case "page":
			page(request,response);
			break;
		}
	}

	private void page(HttpServletRequest request, HttpServletResponse response) {
		Map<String,Object> map=new HashMap<>();
		String name = request.getParameter("departmentname");
		String address = request.getParameter("roomaddress");
		String limit=request.getParameter("limit");
		String curr=request.getParameter("curr");
//		System.out.println(name);

		RoomModel room=new RoomModel();
		room.setRoomAddress(address);
		room.setDepartmentName(name);
		room.setPageLimit(Integer.parseInt(limit));
		room.setPageIndex(Integer.parseInt(curr));
		room.setPageOn(true);
		try {
			map.put("list",service.select(room));
			map.put("count", service.selectCount(room));
			JSONObject jso = new JSONObject(map);
			response.getWriter().print(jso);
		} catch (SQLException | IOException e) {
			e.printStackTrace();
		}
	}

	private void upd(HttpServletRequest request, HttpServletResponse response) {
		String updid=request.getParameter("roomid");
		String updname = request.getParameter("departmentname");
		String updaddress = request.getParameter("roomaddress");
		
		if (updname != null && updaddress != null ) {
			RoomModel room=new RoomModel(Integer.parseInt(updid),updaddress,updname);
			try {
				response.getWriter().write(String.valueOf(service.update(room)));
			} catch (IOException | SQLException e) {
				e.printStackTrace();
			}
		} else {
			try {
				response.getWriter().write("2");
			} catch (IOException e) {
				e.printStackTrace();
			}
		}
		
	}

	private void selectmodel(HttpServletRequest request, HttpServletResponse response) {
		String id=request.getParameter("roomid");
		RoomModel room=new RoomModel();
		room.setRoomId(Integer.parseInt(id));
		try {
			JSONObject jso = new JSONObject(service.selectModel(room));
			response.getWriter().print(jso);
		} catch (SQLException | IOException e) {
			e.printStackTrace();
		}
		
	}

	void del(HttpServletRequest request, HttpServletResponse response) {
		String id=request.getParameter("roomid");
		RoomModel room=new RoomModel();
		room.setRoomId(Integer.parseInt(id));
		try {
			response.getWriter().write(String.valueOf(service.del(room)));
		} catch (SQLException | IOException e) {
			e.printStackTrace();
		}
		
	}

	
	
	void add(HttpServletRequest request, HttpServletResponse response){
		String id=request.getParameter("roomid");
		String name = request.getParameter("departmentname");
		String address = request.getParameter("roomaddress");
		if(name!=null&&address!=null&&id!=null) {
			RoomModel room=new RoomModel(Integer.parseInt(id),address,name);
			try {
				response.getWriter().write(String.valueOf(service.add(room)));
			} catch (IOException | SQLException e) {
				e.printStackTrace();
			}
		}else {
			try {
				response.getWriter().write("2");
			} catch (IOException e) {
				e.printStackTrace();
			}
		}
		
	}
}


